//index.js
//获取应用实例
const app = getApp();
var njzxw = require('../../utils/njzxw.js');
var url = require('../../utils/url.js');
var wxCharts = require('../../utils/wxcharts.js');

Page({
  data: {
    videoContext:null,
    headimg: [],  
    hidevido:true,  
    zhuanjiahide:true,
    tangyouquanhide:true,
    videoSrc:'',
    tyq: {
      path: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535826957797&di=a9f0ef340b93c3d3247c3605d67bae73&imgtype=0&src=http%3A%2F%2Fpic3.nipic.com%2F20090708%2F2082016_090748061_2.jpg',
      image: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535826957797&di=a9f0ef340b93c3d3247c3605d67bae73&imgtype=0&src=http%3A%2F%2Fpic3.nipic.com%2F20090708%2F2082016_090748061_2.jpg',
      remark: '大萨达撒大所大的大萨达撒大所大的大萨达撒大所大的大萨达撒大所大的大萨达撒大所大的大萨达撒大所大的大萨达撒大所大的',
      title: '今天降糖了嘛？',
      name: '张三',
      huifu: '22秒前 回复',
      showNum: '1.2W',
      pinglNum: '1.3W',
      dzNum: '1.2W',
      oper_date: '2012-02-03 12:12:12'
    }
  },
  /**
   * 空值视频播放器的显示
   */
  showVideo:function(e){
    var src = this.data.splist[e.currentTarget.dataset.num].res_path;
    if(src == undefined || src == ''){
      src = this.data.splist[e.currentTarget.dataset.num].url;
    }
    this.setData({
      hidevido:false,
      videoSrc: src
    });
    this.data.videoContext.play();
  },
  /**
   * 空值视频播放器的显示
   */
  hideVideo:function(){
    this.setData({
      hidevido: true
    });
    this.data.videoContext.pause();
  },
  /**
   * 加载导航图
   */
  loadHeadImag:function(){
    var $this = this;
    njzxw.ajax({
      url:url.getItem,
      data:{
        group_id:'APPLET.BAR'
      },
      fun:function(a,b){
        console.log("================" + a);
        if (a) {
          console.log("=========2==========", b.data.data);
          if (b.data.state == 1) {
            var data = [];
            for (var i = 0; i < b.data.data.length;i++){
              var vals = b.data.data[i].values;
              var val = vals.split('##');
              var d = {
                id: b.data.data[i].id,
                name: b.data.data[i].name,
                orderby: b.data.data[i].orderby,
                remark: b.data.data[i].remark,
                values: val[0],
                url: val.length>1?val[1]:null
              };
              data.push(d);
            }
            console.log("=========1==========",data);
            $this.setData({
              headimg: data
            });
          } else {
            njzxw.alert({
              error: true,
              msg: '数据加载异常2'
            });
          }
        } else {
          njzxw.alert({
            error: true,
            msg: '数据加载异常'
          });
        }
      }
    });
    // this.setData({
    //   headimg: njzxw.getCfgVal('APPLET.BAR')
    // });
  },
  loadDataSp:function($this){
    njzxw.ajax({
      url: url.resourcesInfoFront.queryPage,
      data:{
        res_type: 1,
        is_tj:1,
        pageSize: 2,
        pageNum: 1
      },
      isLoad: true,
      fun: function (a, b) {
        if (a) {
          if (b.data.state == 1) {
            $this.setData({
              splist:b.data.data
            });
          } else {
            njzxw.alert({
              error: true,
              msg: '数据加载异常2'
            });
          }
        } else {
          njzxw.alert({
            error: true,
            msg: '数据加载异常'
          });
        }
      }
    });
  },
  loadDataZJ:function($this){
    njzxw.ajax({
      url: url.experinfoFront.queryPage,
      data: {
        is_home: 1,
        pageSize: 2,
        pageNum: 1
      },
      isLoad: true,
      fun: function (a, b) {
        if (a) {
          if (b.data.state == 1) {
            $this.setData({
              zjlist: b.data.data
            });
          } else {
            njzxw.alert({
              error: true,
              msg: '数据加载异常2'
            });
          }
        } else {
          njzxw.alert({
            error: true,
            msg: '数据加载异常'
          });
        }
      }
    });
  },
  loadSHIPU:function($this){
    njzxw.ajax({
      url: url.foodInfoFront.queryPage,
      data: {
        orderKey: 'a.recommend_date desc',
        pageSize: 1,
        pageNum: 1,
      },
      isLoad: true,
      fun: function (a, b) {
        if (a) {
          if (b.data.state == 1) {
            $this.setData({
              shipData: {
                res_path: b.data.data[0].hasOwnProperty('res_path') ? b.data.data[0].res_path : '',
                name: b.data.data[0].title,
                remark: b.data.data[0].remark,
                id: b.data.data[0].id,
              }
            });
          } else {
            njzxw.alert({
              error: true,
              msg: '数据加载异常2'
            });
          }
        } else {
          njzxw.alert({
            error: true,
            msg: '数据加载异常'
          });
        }
      }
    });
  },
  loadbaike: function ($this) {
    njzxw.ajax({
      url: url.encyclopediaInfoFront.queryPage,
      data: {
        orderKey: 'a.oper_date desc',
        pageSize: 2,
        pageNum:1
      },
      isLoad: true,
      fun: function (a, b) {
        if (a) {
          if (b.data.state == 1) {
            $this.setData({
              baikeList: b.data.data
            });
          } else {
            njzxw.alert({
              error: true,
              msg: '数据加载异常2'
            });
          }
        } else {
          njzxw.alert({
            error: true,
            msg: '数据加载异常'
          });
        }
      }
    });
  },
  loaddongtai: function ($this) {
    njzxw.ajax({
      url: url.dynamicInfo.queryPage,
      data: {
        orderKey: 'a.ishost desc,a.oper_date desc',
        pageSize: 1,
        pageNum: 1
      },
      isLoad: true,
      fun: function (a, b) {
        if (a) {
          if (b.data.state == 1) {
            $this.setData({
              dongtaiList: b.data.data
            });
          } else {
            njzxw.alert({
              error: true,
              msg: '数据加载异常2'
            });
          }
        } else {
          njzxw.alert({
            error: true,
            msg: '数据加载异常'
          });
        }
      }
    });
  },
  onShow:function(){
    this.data.videoContext = wx.createVideoContext('vido');
    this.loadHeadImag();
    this.loadDataSp(this);//加载视频
    this.loadDataZJ(this);//加载视频
    this.loadTj(this);//加载统计
    this.loadSHIPU(this);//加载食谱
    this.loadbaike(this);//加载防控百科
    this.loaddongtai(this);//动态

    var expert = njzxw.getCfgVal('CONSULT.EXPERT');
    console.log('===', expert[0].values);
    if (expert[0].values == 1){
      this.setData({
        zhuanjiahide:false 
      });
    }
    var sugar = njzxw.getCfgVal('SUGAR');
    console.log('===', sugar[0].values);
    if (sugar[0].values == 1) {
      this.setData({
        tangyouquanhide: false
      });
    }

  },
  
  loadTj:function(){

    var date = new Date();
    var year = parseInt(date.getFullYear());
    var month = date.getMonth() + 1;
    var date = date.getDate();
    var oper_date_max = year + "-" + month + "-" + date;
    var oper_date_min = njzxw.addDate(year + "-" + month + "-" + date,-7);
    console.log('---:'+oper_date_min);
    var $this = this;
    njzxw.ajax({
      url: url.userArchivesInfo.getOperTj,
      load: true,
      data: {
        oper_date_min: oper_date_min,
        oper_date_max: oper_date_max,
      },
      fun: function (a, b) {
        if (a) {
          $this.setData({
            data: b.data.data
          });
          console.log(b.data.data);
          $this.loadTjTb($this);
          wx.hideLoading();
        } else {
          njzxw.alert({ msg: b.data.msg });
        }
      }
    });
  },
  loadTjTb:function($this){
    new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: $this.data.data.titme,
      series: $this.data.data.blood_sugar,
      yAxis: {
        title: '值（mmol/l）',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: wx.getSystemInfoSync().windowWidth,
      height: 250
    });
  },
  onLoad: function () {
    // console.log('------------------1');
    // wx.getSystemInfo({
    //   success(res) {
    //     console.log('---------------2');
    //     njzxw.alert({
    //       msg: res.version + "--" + res.SDKVersion
    //     });
    //   }
    // })
    // console.log('---------------3');
    njzxw.initFun(this);
  },
  onReady:function(){
    
  },
  onReady: function (res) {
    
  }
})
